﻿#include "stdafx.h"
#include "CountBits.h"

using namespace std;

namespace algorithm {
vector<int> CCountBits::Count(int n)
{
    vector<int> res;

    for (int i = 0; i <= n; i++) {
        int sum = 0;
        int v = i;
        while(v) {
            sum += (v & 1);
            v >>= 1;
            if (v < res.size()) {
                sum += res[v];
                break;
            }
        }
        res.push_back(sum);
    }
    return res;
}
}
